-- UIMopUpEffect
-- Created by huanghr
-- 开启扫荡动画

require "game/ui/module/hero/HeroAnimation"

UIMopUpEffect = class("UIMopUpEffect", function()
    return cc.Layer:create();
end);

function UIMopUpEffect.create(duration, callback)
    return UIMopUpEffect.new(duration, callback);
end

-- 构造函数
function UIMopUpEffect:ctor(duration, callback)
    self:setName("UIMopUpEffect");
    local node = cc.CSLoader:createNode("layout/main/MopUpEffects.csb");
    self:addChild(node);
    self.node = node;
    self:resize();

    -- 加载动画
    self:createSlimeAnimation();

    self:braveSlimeFlyIn();
end

-- 适配
function UIMopUpEffect:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end

function UIMopUpEffect:createSlimeAnimation()
    local modelNode = findChildByName(self.node, "CT/hero_model")
    modelNode:removeAllChildren();

    local modelId = PetM.query(4, "default_model");
    local animName = PetM.queryModel(modelId, "animation");
    local animation = HeroAnimation.create("slime_animation/" .. animName .. ".csb");
    self.animation = animation;
    modelNode:addChild(self.animation);
    modelNode:setScale(0.5);
    modelNode:setVisible(false);

    self.modelNode = modelNode;

    local modelNode2 = findChildByName(self.node, "CT/hero_model2");
    self.modelNode2 = modelNode2;
end

-- 冈布奥头像飞入
function UIMopUpEffect:braveSlimeFlyIn(callback)
    -- 强盗冈布奥飞入
    local headIcon = findChildByName(self.node, "CT/hero_icon");
    local iconPath = getHeroLargeIconPath(PetM.getDefaultLargeIcon(4));
    headIcon:loadTexture(iconPath);

    local oldPos = cc.p(headIcon:getPosition());
    headIcon:setPositionX(oldPos.x + DESIGN_WIDTH);
    local move1 = cc.MoveTo:create(0.2, cc.p(oldPos.x - 10, oldPos.y));
    local move2 = cc.MoveTo:create(0.2, cc.p(oldPos.x, oldPos.y));
    local delay = cc.DelayTime:create(0.1);
    local delay2 = cc.DelayTime:create(0.5);

    -- 获取帧范围和播放速度
    local modelId = PetM.query(4, "default_model");
    local frameRange = PetM.queryModel(modelId, "attack_frame");
    local speed = PetM.queryModel(modelId, "attack_speed");

    local callFunc = cc.CallFunc:create(function()
        headIcon:setVisible(false);
        self.modelNode:setVisible(true);
       -- 播放
        self.animation:play(HERO_ACTION_ATTACK, false, frameRange[1], frameRange[2], speed);
    end);

    local callFunc2 = cc.CallFunc:create(function()
          self.modelNode2:removeAllChildren();
          playEffect(self.modelNode2, 1278, 0, -50);
    end);

    headIcon:runAction(cc.RepeatForever:create(cc.Sequence:create(move1, move2, delay, callFunc, delay2, callFunc2)));
end

